import { InfoFilled, WarningFilled, CircleCheckFilled } from "@element-plus/icons-vue"
import type  { EpPropMergeType } from "element-plus/es/utils/vue/props/types"
import { ElNotification } from 'element-plus'
import { h } from 'vue'

export interface Message {
    id: string          // ID
    title: string       // 标题
    content: string     // 内容
    type: string        // 消息类型
    time: string        // 消息时间
    isRead: boolean     // 是否已读
}

export class MessageType {
    static get(message: Message | undefined) {
        switch (message?.type) {
            case 'info': return {
                icon: InfoFilled, 
                class: 'info-icon', 
                tagType: 'info' as EpPropMergeType<StringConstructor, "success" | "warning" | "info" | "primary" | "danger", unknown>, 
                label: '通知'
            }
            case 'warning': return {
                icon: WarningFilled, 
                class: 'warning-icon', 
                tagType: 'warning' as EpPropMergeType<StringConstructor, "success" | "warning" | "info" | "primary" | "danger", unknown>, 
                label: '警告'
            }
            case 'success': return {
                icon: CircleCheckFilled, 
                class: 'success-icon', 
                tagType: 'success' as EpPropMergeType<StringConstructor, "success" | "warning" | "info" | "primary" | "danger", unknown>, 
                label: '成功'
            }
            default: return {
                icon: InfoFilled, 
                class: 'info-icon', 
                tagType: 'info' as EpPropMergeType<StringConstructor, "success" | "warning" | "info" | "primary" | "danger", unknown>, 
                label: '通知'
            }
        }
    }
}

const key = "messages"

export class MessageUtils {

    static getMessages(): Message[] {
        return JSON.parse(localStorage.getItem(key) || '[]')
    }

    static setMessages(messages: Message[]) {
        localStorage.setItem(key, JSON.stringify(messages))
    }
    
    static setRead(id: string) {
        const messages = this.getMessages()
        const index = messages.findIndex(item => item.id == id)
        messages[index].isRead = true
        this.setMessages(messages)
    }

    static clear() {
        localStorage.removeItem(key)
    }

    static addMessage(message: Message, success: Function) {
        const messages = this.getMessages()
        messages.push(message)
        this.setMessages(messages)
        ElNotification({
            title: message.title,
            message: h('i', { style: 'color: teal' }, message.content),
            position: 'top-left',
            showClose: false,
        })
        success(messages)
    }


}